package com.blockchain.securewallet.view;

import android.content.Context;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;

import com.blockchain.securewallet.utils.ToastUtils;
import com.blockchain.securewallet.utils.ToolUtils;


/**
 * Created by Lopez on 2018/5/28.
 */
public class ETMoneyView extends android.support.v7.widget.AppCompatEditText {

    private static String mText;

    public ETMoneyView(Context context) {
        super(context);
        setChangeListener();
    }


    public ETMoneyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setChangeListener();
    }

    public ETMoneyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setChangeListener();
    }


    //设置addTextChangedListener监听
    private void setChangeListener() {
        this.addTextChangedListener(mTextChangedListener);
    }


    private final TextWatcher mTextChangedListener = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mText = s.toString();

            if (TextUtils.isEmpty(mText)) return;

            if (!ToolUtils.isMoneyNumber(mText)) {

                if (mText.contains(".") && mText.indexOf(".") == 0) {
                    ToastUtils.showToast("第一个不能为小数点");
                    return;
                }

                //进入小数点的判断情况
                if (mText.indexOf(".") != mText.lastIndexOf(".")) {
                    ToastUtils.showToast("不能包含2个以上的小数点");
                    return;
                }

                if (mText.contains(".") && mText.lastIndexOf(".") == mText.length() - 1) {
                    //因为是输入每个都监听，所以对最后1个为小数点，暂不提示
                } else {
                    ToastUtils.showToast("金额格式不正确");
                }
            }
        }

    };

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

}
